package com.csweimei.rentmanagementtreasure.tenant.viewModel;

import android.app.Application;
import android.content.Intent;
import android.graphics.PointF;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import androidx.recyclerview.widget.RecyclerView;

import com.csweimei.rentmanagementtreasure.R;
import com.csweimei.rentmanagementtreasure.adapter.HouseHzAdapter;
import com.csweimei.rentmanagementtreasure.base.AppConfig;
import com.csweimei.rentmanagementtreasure.http.postBean.PostHouseHzBean;
import com.csweimei.rentmanagementtreasure.http.postBean.PostRoomBean;
import com.csweimei.rentmanagementtreasure.pop.PopUtils;
import com.csweimei.rentmanagementtreasure.pop.PopWheelBase;
import com.csweimei.rentmanagementtreasure.utils.ValidateUtils;
import com.csweimei.rentmanagementtreasure.viewModel.TitleVM;
import com.google.gson.Gson;
import com.gtdev5.geetolsdk.mylibrary.util.LogUtils;
import com.gtdev5.geetolsdk.mylibrary.util.ToastUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;

public class ImproveVM extends TitleVM {
    public ObservableField<String> Payment = new ObservableField<>();
    public ObservableField<String> houseType = new ObservableField<>("2室0厅0厨0卫");

    public ObservableField<PostHouseHzBean> postHouseHzBean = new ObservableField<>();

    List<PostRoomBean> list = new ArrayList<>();

    public ImproveVM(@NonNull Application application) {
        super(application);
        setTitleText("完善信息");
    }



    public BindingCommand houseTypeClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            PopWheelBase popWheelBase = new PopWheelBase(mActivity, 4, s ->{
                houseType.set(s);
                String[] strings = s.split("-");
                setAdapter(strings[0].replace("室",""));


            });
            PopUtils.showPop(mActivity,popWheelBase,false);

            popWheelBase.setList1(Arrays.asList(mActivity.getResources().getStringArray(R.array.houseTypeW1)));
            popWheelBase.setList2(Arrays.asList(mActivity.getResources().getStringArray(R.array.houseTypeW2)));
            popWheelBase.setList3(Arrays.asList(mActivity.getResources().getStringArray(R.array.houseTypeW3)));
            popWheelBase.setList4(Arrays.asList(mActivity.getResources().getStringArray(R.array.houseTypeW4)));
        }
    });
    HouseHzAdapter adapter;

    private void setAdapter(String string) {
        list.clear();
        adapter = new HouseHzAdapter();
        recyclerView.setAdapter(adapter);
        for (int i = 0; i < Integer.valueOf(string); i++) {
            PostRoomBean postRoomBean = new PostRoomBean();
            list.add(postRoomBean);
        }
        adapter.setList(list);
    }

    public BindingCommand paymentClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            PopWheelBase popWheelBase = new PopWheelBase(mActivity, 1, s -> Payment.set(s));
            PopUtils.showPop(mActivity,popWheelBase,false);

            List<String> list1 = new ArrayList<>();
            list1.add("现金");
            list1.add("微信");
            list1.add("支付宝");
            list1.add("其他");


            popWheelBase.setList1(list1);
        }
    });

    RecyclerView recyclerView;
    public void setRecy(RecyclerView recyHouse) {
        recyclerView = recyHouse;
        setAdapter("2");

    }

    public BindingCommand postData = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if (ValidateUtils.isEmptyString(Payment.get())){
                ToastUtils.showShortToast("请选择支付方式");
                return;
            }
            for (PostRoomBean room:adapter.getData()) {
                if (ValidateUtils.isEmptyString(room.room)
                || ValidateUtils.isEmptyString(room.rent)
                || ValidateUtils.isEmptyString(room.deposit)){
                    ToastUtils.showShortToast(room.num+"信息未填写");
                    return;
                }
            }
            LogUtils.e(new Gson().toJson(adapter.getData()));



            postHouseHzBean.get().setPayment(Payment.get());
            postHouseHzBean.get().setHouseType(houseType.get());
            postHouseHzBean.get().setRoomjson(new Gson().toJson(adapter.getData()));
            Intent intent = new Intent();
            intent.putExtra(AppConfig.BUNDLE_KEY,postHouseHzBean.get());
            mActivity.setResult(10087,intent);
            finish();

        }
    });

}
